﻿using Sidvall.Data;
using Sidvall.WorkItemManager.Workflow;
using System.Threading.Tasks;

namespace Sidvall.WorkItemManager.Model.DataPortalManagers
{
    public partial class WorkItemLogManager<TEntity, TList>
    {
        #region Public Members

        #region DeleteItemsByWorkItemAsync

        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkItemAsync(int serverConnectionId, long workItemId)
        {
            global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemLogField> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewDeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemLogField>();
            criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemLogField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemLogField.WorkItemId, Sidvall.Data.ComparisonOperator.Equal, workItemId);
            ParameterManager.InitializeParameters(criteria, serverConnectionId);

            return await DeleteItemsAsync(criteria).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsAsync

        public async Task<TList> GetItemsAsync(int? serverConnectionId, long? workItemId, string textFilter, bool? messageIsEmpty, WorkItemResultType? workItemResultType,
            bool reversedSort, int? rowCount, int? rowStartIndex)
        {
            FetchListCriteria<Data.Fields.WorkItemLogField, Data.Relations.WorkItemLogRelation> criteria;

            criteria = new FetchListCriteria<Data.Fields.WorkItemLogField, Data.Relations.WorkItemLogRelation>();
            criteria.RowCount = rowCount;
            criteria.RowStartIndex = rowStartIndex;
            criteria.SortItems = new SortItemCollection<Data.Fields.WorkItemLogField>();
            if (reversedSort)
            {
                criteria.SortItems.Add(Data.Fields.WorkItemLogField.ExecutionEndTime, false);
            }
            else
            {
                criteria.SortItems.Add(Data.Fields.WorkItemLogField.ExecutionEndTime, true);
            }
            criteria.Filter = new Filter<Data.Fields.WorkItemLogField>();
            if (workItemId != null)
            {
                criteria.Filter.AddFilterItem(Data.Fields.WorkItemLogField.WorkItemId, ComparisonOperator.Equal, workItemId.Value);
            }
            if (!string.IsNullOrWhiteSpace(textFilter))
            {
                criteria.Filter.AddFilterItem(Data.Fields.WorkItemLogField.ResultMessage, ComparisonOperator.Like, textFilter);
            }
            if (messageIsEmpty == true)
                criteria.Filter.AddFilterItem(Data.Fields.WorkItemLogField.ResultMessage, ComparisonOperator.Equal, string.Empty);
            else if (messageIsEmpty == false)
                criteria.Filter.AddFilterItem(Data.Fields.WorkItemLogField.ResultMessage, ComparisonOperator.NotEqual, string.Empty);
            if (workItemResultType != null)
            {
                criteria.Filter.AddFilterItem(Data.Fields.WorkItemLogField.WorkItemResultId, ComparisonOperator.Equal, workItemResultType.Value);
            }
            ParameterManager.InitializeParameters(criteria, serverConnectionId);

            return await GetItemsAsync(criteria).ConfigureAwait(false);
        }

        #endregion

        #endregion
    }
}